package com.xmu.prcs.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xmu.prcs.dao.KindShuDao;
import com.xmu.prcs.entity.KindShu;

/**
 * 
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
public class KindShuDaoImpl extends GenericDaoImpl<KindShu> implements KindShuDao{

	public KindShuDaoImpl() {
		super(KindShu.class);
	}

	public List<KindShu> findKindShuByPropety(String propety, Object param) {
		List<KindShu> lists = null;
		try{
			String sql = "from KindShu as model where model." + propety + " = ?";
			lists = this.getHibernateTemplate().find(sql, param);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}catch(Throwable t){
			t.printStackTrace();
			return null;
		}
		return lists;
	}
	
	public int countKindShu(int idKindKe, String kindName){
		String sql = "select count(1) from kindshu shu where shu.idkindke='" + idKindKe + "'";
		if(kindName != null && !kindName.equals("")){
			sql += " and shu.kindname like '%" + kindName + "%'";
		}
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql);
		int size;
		if(query.uniqueResult() == null){
			size = 0;
		}
		size = Integer.parseInt((query.uniqueResult().toString()));
		System.out.println("size:" + size);
		return size;
	}

	public List<KindShu> findKindShu4Page(int begin, int size, int idKindKe, String kindName){
		String sql = "select * from kindshu shu where shu.idkindke='" + idKindKe + "'";
		if (kindName != null && !kindName.equals("")) {
			sql += " and shu.kindName like '%" + kindName + "%'";
		}
		Query query = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).addEntity(KindShu.class);
		query.setFirstResult(begin);
		query.setMaxResults(size);

		return query.list();
	}
}
